import Vue from "vue";
import axios from "axios";
import store from '../vuex/index';

let baseUrl = AppConfig.hUrl;
//区别开发环境和生产环境
if (process.env.NODE_ENV === 'development') {
    baseUrl = '/api';
};

axios.defaults.withCredentials = true;

axios.interceptors.request.use(
    config => {
        const token = getStore('token')
        var signMsg = getStore('signMsg')
        //console.log(getStore('signMsg'))
        config.data = JSON.stringify(config.data)
        config.headers = {
            'Content-Type': 'application/json',
            "token": token,
            // "signMsg": signMsg
        }
        return config
    },
    err => {
        return Promise.reject(err)
    }
)
// http response 拦截器
axios.interceptors.response.use(
    response => {
        console.log(response);
        if (response.data.code == 413) {
            console.log("失效");
            console.log(response.data);
            //Toast(response.data.msg);
            //清除store
            // store.dispatch("hexitSystem");
            //
            window.location.href = "#/login";
        }
        return response;
    },
    error => {
        if (error.response) {
            console.log(error.response);
        }
        return Promise.reject(error.response.data)
    });


/**
 * http请求 post
 * @param httpURL
 * @param data
 * @param fn
 */
function Post(url, data, callBack, next) {
    let common = {
        version: "1.0.0",
        softType: "qgc_h5",
        tokenId: localStorage.getItem("tokenId"),
        userId: localStorage.getItem("userId"),
    };
    var obj = Object.assign(data, common);
    if (!obj.hasOwnProperty("interfaceVersion")) {
        //obj.interfaceVersion = "v1";
    }
    let hurl = baseUrl + url;
    axios
        .post(hurl, obj)
        .then(res => {
            console.log("funCode>>>>>>" + obj.funCode);
            if (res.data.retCode != "0000") {
                if (res.data.retCode == "2001") {
                    //alert(); store.dispatch("hexitSystem");
                    window
                        .localStorage
                        .removeItem("userId");
                    window
                        .localStorage
                        .removeItem("tokenId");
                    window
                        .localStorage
                        .removeItem("userInfo");
                    window.location.href = "#/login";
                } else {
                    hToast("错误");
                }
            } else {
                callBack(res.data);
            }
            //针对多个api异步调用时
            if (typeof next != "undefined") {
                next(res.data);
            }
        })
        .catch(function (error) {
            hToast("系统错误,请稍后重试");
        });
}
/**
 * http请求 post
 * @param httpURL
 * @param data
 * @param fn
 */
function Get(url, data, callBack, next) {
    let common = {
        mobileType: "3",
        tokenId: store.state.common.tokenId,
        userId: store.state.common.userId,
    };
    var obj = Object.assign(data, common);
    if (!obj.hasOwnProperty("interfaceVersion")) {
        // obj.interfaceVersion = "v1";
    }
    singMsgT(obj); //验签
    console.log("验签");
    console.log(obj);
    let hurl = baseUrl + url + hstringify(obj); //验签;
    axios
        .get(hurl)
        .then(res => {
            console.log(res);
            if (res.data.code != "200") {
                hToast(res.data.msg);
            } else {
                callBack(res.data);
            }
            //针对多个api异步调用时
            if (typeof next != "undefined") {
                next(res.data);
            }
        })
        .catch(function (error) {
            console.log(error);
            hToast("系统错误,请稍后重试");
        });
}
export default {
    Post,
    Get
};